using PEMath;
using UnityEngine;

/****************************************************
// 功能：主要逻辑单位（英雄、小兵、塔）
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/28 16:29:3
*****************************************************/

public enum UnitStateEnum
{
    Alive,
    Dead
}

public enum UnitTypeEnum
{
    Hero,
    Soldier,
    Tower
}

public enum TeamEnum
{
    None,
    Blue,
    Red
}

public abstract partial class MainLogicUnit : LogicUnit
{
    public LogicUnitData unitData;
    public UnitStateEnum unitState;
    public UnitTypeEnum unitType;

    public MainViewUnit mainViewUnit;

    
    protected string pathPrefix = "";
    
    protected MainLogicUnit(LogicUnitData unitData)
    {
        this.unitData = unitData;
        unitName = unitData.unitConfig.unitName;
    }
    
    public override void LogicInit()
    {
        // 初始化属性
        InitProperties();
        
        // 初始化技能
        InitSkill();
        
        // 初始化移动
        InitMove();
        
        GameObject game = ResourceManager.Instance.LoadPrefab(pathPrefix + "/" + unitData.unitConfig.resName, null, true);
        mainViewUnit = game.GetComponent<MainViewUnit>();
        if (mainViewUnit == null)
        {
            Debug.LogError("Get MainViewUnit Error：" + unitName);
        }
        mainViewUnit.Init(this);

        unitState = UnitStateEnum.Alive;
    }

    public override void LogicTick()
    {
        TickSkill();
        TickMove();
    }

    public override void LogicUnInit()
    {
        UnitSkill();
        UninitMove();
    }

    public void InputKey(OpKey key)
    {
        switch (key.keyType)
        {
            case KeyType.Skill:
                InputSkillKey(key.skillKey);
                break;
            case KeyType.Move:
                var x = PEInt.zero;
                x.ScaledValue = key.moveKey.x;
                var z = PEInt.zero;
                z.ScaledValue = key.moveKey.z;
                InputMoveKey(new PEVector3(x, 0,z));
                break;
            case KeyType.None:
                break;
            default:
                Debug.LogError("KEY is not exist");
                break;
        }
    }

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="audioName"></param>
    /// <param name="loop"></param>
    /// <param name="delay"></param>
    public void PlayAudio(string audioName, bool loop = false, int delay = 0)
    {
        mainViewUnit.PlayAudio(audioName, loop, delay);
    }

    /// <summary>
    /// 播放动画
    /// </summary>
    /// <param name="aniName"></param>
    public void PlayAni(string aniName) { mainViewUnit.PlayAni(aniName); }

    public virtual bool IsPlaySelf() { return false; }

    /// <summary>
    /// 用于子类重写，判定传入逻辑单位是否相等
    /// </summary>
    /// <param name="mainLogicUnit"></param>
    /// <returns></returns>
    public virtual bool Equals(MainLogicUnit mainLogicUnit)
    {
        return false;
    }
}
